home *** CD-ROM | disk | FTP | other *** search
/ Gold Medal Software 3 / Gold Medal Software - Volume 3 (Gold Medal) (1994).iso / print / bjfilt.arj / BJFILT.C next >
Text File  |  1994-02-03  |  9KB  |  287 lines

  1. /* bjfilt.c
  2.  *
  3.  *Canon BJ200 filter to remove extra "ab" from printouts
  4.  * Author: W.G. Unruh <unruh@physics.ubc.ca> 
  5.  * Based on printmon.c by  Kai Uwe Rommel <rommel@jonas>
  6.  * Created:Jan 1994
  7.  *
  8.   */
  9.  
  10. static char *rcsid =
  11. "$Id: bjfilt.c,v 1.0 1994/1/29   Exp $";
  12. static char *rcsrev = "$Revision: 1.0 $";
  13.  
  14.  
  15. #define   INCL_DOSFILEMGR
  16. #define   INCL_DOSMONITORS
  17. #define   INCL_DOSPROCESS
  18. #include <os2.h>
  19. #ifdef __EMX__
  20. #include <mon16.h>
  21. #endif
  22.  
  23. #include <stdio.h>
  24. #include <string.h>
  25. #include <strings.h>
  26. #include <fcntl.h>
  27. #include <process.h>
  28. #include <malloc.h>
  29.  
  30. #ifdef M_I86
  31. #define popen _popen
  32. #define pclose _pclose
  33. #endif
  34.  
  35. #ifdef DEBUG
  36. #define dprintf(f, p) printf(f, p)
  37. #else
  38. #define dprintf(f, p)    
  39. #endif
  40.  
  41. #ifdef ERROR                               /* To print out error messages */
  42. #define eprintf(f, p) printf(f, p)
  43. #else
  44. #define eprintf(f, p)    
  45. #endif
  46.  
  47. #pragma pack(1)     /* To line stuff up on byte rather than dword boundaries */
  48.  
  49. struct buffer
  50. {
  51.   short length;
  52.   char data[158];
  53. };
  54.  
  55. struct packet
  56. {
  57.   unsigned char mflag;
  58.   unsigned char dflag;
  59.   short label;
  60.   char wdata[138];
  61. };
  62. int packetprolog=4;
  63.  
  64. #pragma pack()
  65.  
  66. /* Prototype of Routine to find filter string in received data
  67.   *  Cannot use the string fuctions as they interpret a Null as the end of the string
  68. */
  69.  int findstr( char *string1,int n1,char *string2, int n2);
  70.  
  71. /* Filter string and length Note that this is the set of escape sequences that the
  72.  * BJ200 interprets as ab which is tacked on to the end of most print jobs
  73. *  Esc ( a 0x01 Null Null Esc ( b 0x01 Null Null Esc @
  74. */
  75. char *filtstr= "\x1b(a\x01\x00\x00\x1b(b\x01\x00\x00\x1b@";
  76. char *outstr="\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x1b@";
  77. int filtlen=14;
  78.  
  79. void main(int argc, char **argv)
  80. {
  81.   USHORT usRC, cbData;
  82.   ULONG cbWritten;
  83.   HMONITOR mon;
  84.   struct buffer ibuffer, obuffer;
  85.   struct packet wbuffer;
  86.   char port[16] ;
  87.   int monitor=0;                                               /* print out all data bytes */
  88.   int verbose=1;                                                /* Print out statistics? */
  89.   int replace=1;                                                 /*replace filter string? */
  90.   int numberReplaced=0;                                /* Number of times filter string replaced */
  91.   int overlay;                                                   /* excess string length*/
  92.   char woverlay[20];                                      /* excess string buffer */
  93.   
  94.   if (argc < 2)
  95.   {
  96.     printf("\nUsage:\tBJFILT <port>  [monitor] [quiet] [noreplace]\n\n"
  97.                               "\t<port>  = port with BJ200 driver attached.\n" 
  98.                               "\tmonitor   = if present dumps filtered troughput out stdout as well\n"
  99.                                 "\tquiet        = if present, do not give statistics of run\n"
  100.                                "\tnoreplace = Do not replace the filter string.\n"
  101.                                                        "\t\t\t Makes no sense without the  monitor flag \n"
  102.                                                     " \t [flag] means that flag is optional\n\n");
  103.     exit(1);
  104.   }
  105.  
  106.     strcpy(port, argv[1]);
  107.    {
  108.       int i;
  109.       for  (i=2;i<argc;i++)
  110.       {
  111.          if (!(strcmp("monitor",argv[i]))) { monitor=1; continue;}
  112.          if (!(strcmp("quiet",argv[i]))) {verbose=0; continue;}
  113.          if (!(strcmp("noreplace",argv[i]))){replace=0; continue;}
  114.        }
  115.     }
  116.  
  117.   if (verbose == 1)  printf("\nBJFILT: Canon BJ200 'ab' filter monitor\n");
  118.  
  119.   
  120.   usRC = DosMonOpen(port, &mon);
  121.  
  122.   if (usRC != 0)
  123.   {
  124.      printf("DosMonOpen error code: %u\n", usRC);
  125.      if (usRC=380) printf ("Invalid port %s\n",port);
  126.      exit(usRC);
  127.   }
  128.  
  129.   ibuffer.length = obuffer.length = sizeof(struct buffer);
  130.  
  131.  
  132.   usRC = DosMonReg(mon, (PBYTE) &ibuffer, (PBYTE) &obuffer, MONITOR_END, 1);
  133.    if (usRC)
  134.   {
  135.      eprintf("DosMonReg error code: %u\n", usRC);
  136.      exit(usRC);
  137.   }
  138.  
  139.  
  140.  /* Main Loop */
  141.   while (usRC == 0)
  142.   {
  143.     cbData = sizeof(struct packet);
  144.     usRC = DosMonRead((PBYTE) &ibuffer, 0, (PBYTE) &wbuffer, &cbData);
  145.  
  146.     if (usRC)
  147.     {
  148.       eprintf("DosMonRead error code: %u\n", usRC);
  149.       break;
  150.     }
  151.  
  152.  
  153.    if(verbose == 1)
  154.    {
  155.        if (wbuffer.mflag & 1)
  156.       {
  157.         dprintf("monitor: open\n", 0);
  158.         cbWritten = 0;
  159.        }
  160.  
  161.        if (wbuffer.mflag & 2)
  162.       {
  163.          dprintf("\nmonitor: close\n", 0);
  164.          printf("bjfilt:Number bytes written=%ld  Number of filter strings replaced=%d\n", 
  165.                                               cbWritten, numberReplaced);
  166.       }
  167.  
  168.        if (wbuffer.mflag & 4)
  169.       {
  170.          dprintf("monitor: flush\n", 0);  
  171.       }
  172.  
  173.       if (wbuffer.dflag == 32)
  174.       {
  175.           printf("bjfilt:Job Name= %s\n", wbuffer.wdata + sizeof(short));
  176.        }
  177.        if (wbuffer.dflag == 0)
  178.        {
  179.           cbWritten += cbData - packetprolog;
  180.           dprintf("\rmonitor: %lu data bytes", cbWritten);
  181.        }
  182.   }   /* end if verbose */
  183.  
  184.     if((wbuffer.dflag&&63)==0 &&(wbuffer.mflag&7)==0)       /*data packet-- Upper parts 
  185.                                                                                                          of bytes reserved */
  186.      {
  187.         int i;
  188.         int loc=-1;
  189.         if(overlay>0)      /* shift the input over*/
  190.               {
  191. #ifdef DEBUG
  192.                 printf("\n Before the move=");
  193.                 for (i=0;i<cbData;i++) putchar(wbuffer.wdata[i]);
  194. #endif
  195.                  for(i=0;i<cbData-packetprolog;i++)
  196.                            {  wbuffer.wdata[cbData-packetprolog-1+overlay-i]
  197.                                                       =wbuffer.wdata[cbData-packetprolog-1-i];
  198.                             }
  199.                     cbData+=overlay;
  200. #ifdef DEBUG
  201.                 printf("\n After the move="); 
  202.                 for (i=0;i<cbData;i++) putchar(wbuffer.wdata[i]);putchar('\n');
  203. #endif
  204.                    for(i=0;i<overlay;i++)  wbuffer.wdata[i]=woverlay[i];     /* Inset the saved string*/
  205. #ifdef DEBUG
  206.                 printf("\n After the replace=");
  207.                 for (i=0;i<cbData;i++) putchar(wbuffer.wdata[i]);putchar('\n');
  208. #endif
  209.                  }
  210.         /*Search the data for the desired string */
  211.         if (replace)
  212.           {
  213.              while ((loc =findstr(filtstr, filtlen,wbuffer.wdata,cbData-packetprolog))>=0 
  214.                                           &&loc<=cbData-filtlen-packetprolog)
  215.                 { 
  216.                    for(i=0;i<filtlen;i++) wbuffer.wdata[loc+i]=outstr[i] ;     /* replace the found string*/
  217.                     numberReplaced++;
  218. #ifdef DEBUG
  219.                    printf( "\nString Found  loc=%d, cbData=%d\n", loc,cbData);
  220. #endif 
  221.                 }
  222.             }
  223.          if(loc>=0&&loc<cbData-packetprolog)              /* Tail has partial match */
  224.                { 
  225.                  overlay= cbData-loc-packetprolog;          /* match length*/
  226.                  cbData=loc+packetprolog;                        /* Data to be sent this time */
  227.                  for (i=0;i<overlay;i++) woverlay[i]=wbuffer.wdata[i+loc];           /*save rest*/
  228. #ifdef DEBUG
  229.                  printf("\noverlay length=%d,cbData=,%d,loc=%d overlay=",overlay,cbData,loc);
  230.                  for (i=0;i<overlay;i++) putchar(woverlay[i]); putchar('\n');
  231. #endif
  232.                 }
  233.         else overlay=0;
  234.        if(monitor!=0)                               /* Print out output if monitoring */
  235.                {
  236.                   char* j ;
  237.                   j= wbuffer.wdata;
  238.                   dprintf("\nData length %d\n" , cbData-packetprolog);
  239.                  for (i=packetprolog;i<cbData;i++)  putchar( *(j++));
  240.                }
  241.      }              /*end of  if data packet*/
  242.  
  243.       /*send back all packets received */
  244.        if ( cbData >128+packetprolog)                         /*Is packet too long?*/
  245.          { 
  246.             int i, tail;                                                       /*send first 128 bytes*/
  247.             tail = cbData-128-packetprolog;
  248.             cbData=128+packetprolog;
  249. #ifdef DEBUG
  250.       {int ii;
  251.       printf("\nActual Output, file id=%d\n",(int)wbuffer.label);
  252.      for (ii=0;ii<cbData-packetprolog;ii++) putchar(wbuffer.wdata[ii]); putchar('\n');
  253.       }
  254. #endif 
  255.             usRC=DosMonWrite((PBYTE)&obuffer,(PBYTE)&wbuffer, cbData);
  256.             if(usRC!=0)
  257.                { eprintf("DosMonWrite error return %d",usRC);
  258.                  break;
  259.                }
  260.             for(i=0;i<tail;i++)
  261.               {wbuffer.wdata[i]=wbuffer.wdata[128+i];}       /* move back rest of packet*/
  262.             cbData=tail+packetprolog;
  263.           }
  264.  
  265.                                                                                       /* Send remainder of packet*/ 
  266.       usRC=DosMonWrite((PBYTE)&obuffer,(PBYTE)&wbuffer, cbData);   
  267. #ifdef DEBUG
  268.      {int ii;
  269.      printf("\nActual Output, file id=%d\n", (int) wbuffer.label);
  270.      for (ii=0;ii<cbData-packetprolog;ii++) putchar(wbuffer.wdata[ii]);  putchar('\n');
  271.      }
  272. #endif
  273.       if(usRC!=0)
  274.            { eprintf("DosMonWrite error return %d",usRC);
  275.              break;
  276.            }
  277.   }
  278.  
  279.   dprintf("monitor: closing\n", 0);
  280.  
  281.   DosMonClose(mon);
  282.  
  283.   exit(usRC);
  284. }
  285.  
  286. /* end of bjfilt.c */
  287.